package com.michaelbulava.TFS.ui.dialogs.vc;

import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.michaelbulava.TFS.ui.controls.vc.ServerItemTreeControl;
import com.michaelbulava.TFS.ui.controls.vc.ServerPathSelectionControl;
import com.michaelbulava.TFS.ui.dialogs.BaseDialog;
import com.michaelbulava.TFS.vc.tfsitem.TFSItem;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;
import com.microsoft.tfs.util.Check;
import com.microsoft.tfs.util.valid.ValidityChangedEvent;
import com.microsoft.tfs.util.valid.ValidityChangedListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * Created by MichaelA on 7/27/2015.
 */
public class ServerItemTreeDialog extends BaseDialog {
    private ServerPathSelectionControl treeControl;
    private String title;
    private String selectedPath;

    //private final Project myProject;
    private final Workspace myWorkspace;
    private final String projectOrModuleName;

    public ServerItemTreeDialog(@Nullable Project project, Workspace workspace, String title,
                                String ProjectOrModuleName) {
        super(project);
        this.title = title;
        //Check.notNull(project, "project");
        Check.notNull(workspace, "workspace");
        //this.myProject = project;
        this.myWorkspace = workspace;
        this.projectOrModuleName = ProjectOrModuleName;
    }

    public ServerItemTreeDialog(@NotNull Component parent, Project project, Workspace workspace, String title,
                                String ProjectOrModuleName) {
        super(parent);
        this.title = title;
        //Check.notNull(project, "project");
        Check.notNull(workspace, "workspace");
        //this.myProject = project;
        this.myWorkspace = workspace;
        this.projectOrModuleName = ProjectOrModuleName;
    }

    @Override
    protected JComponent createControl() {
        if (treeControl == null){
            final JPanel panel1 = new JPanel();
            panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
            final JBScrollPane scrollPane1 = new JBScrollPane();
            panel1.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0,
                    false));
            treeControl = new ServerPathSelectionControl(projectOrModuleName);
            //treeControl.setProject(myProject);
            treeControl.setWorkspace(myWorkspace);
//            treeControl.setServerPathSelectionChangedListener(new ServerItemTreeControl.ServerPathSelectionChangedListener() {
//                @Override
//                public void OnTreeSelectionChanged(TFSItem selection) {
//                    selectedPath = selection;
//
//                }
//            });

            treeControl.getValidator().addValidityChangedListener(new ValidityChangedListener() {
                @Override
                public void validityChanged(ValidityChangedEvent validityChangedEvent) {
                    if (validityChangedEvent.getValidator().getValidity().isValid())
                        selectedPath = treeControl.getSelectedPath();
                    else
                        selectedPath = null;
                }
            });

            scrollPane1.setViewportView(treeControl);
            return panel1;
        } else {
            throw new IllegalArgumentException("?????");
        }
    }

    @Override
    protected String dialogTitle() {
        return title;
    }

    public String getSelectedPath(){
        return selectedPath;
    }

    @Override
    protected Action[] createActions() {
        return defaultActions();
    }
}
